package BeheerFysiotherapeuten.View;

import BeheerFysiotherapeuten.Model.PraktijkLijst;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import java.util.Observable;
import java.util.Observer;

public class TherapieOverzicht extends javax.swing.JPanel implements Observer {
	private Object[][] inhoud;
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JButton jButton3;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable jTable1;
	private PraktijkLijst  pratijklijst;

	

	public TherapieOverzicht() {

	

		initComponents();

	}

	public void setListener(ActionListener listener) {

		jButton1.addActionListener(listener);
		jButton2.addActionListener(listener);
		jButton3.addActionListener(listener);
		
	

	}

	public Object[][] getInhoud() {
		return inhoud;

	}

	public void setInhoud(Object[][] inhoud) {
		this.inhoud = inhoud;

		jTable1.setModel(new javax.swing.table.DefaultTableModel(inhoud,
				new String[] { "Behandelcode", "Therapienaam","Therapeut","TherapieSoort", "Duur",
						"Prijs" ,"Omschrijving"}));


	}

	private void initComponents() {
		jButton1 = new javax.swing.JButton();
		jButton2 = new javax.swing.JButton();
		jButton3 = new javax.swing.JButton();

		jButton1.setActionCommand("Nieuw");

		jButton2.setActionCommand("Wijzig");

		jButton3.setActionCommand("Verwijder");

		jScrollPane1 = new javax.swing.JScrollPane();

		jTable1 = new javax.swing.JTable(){
			public boolean isCellEditable(int row, int coll){
				return false;
			}
		};
		jTable1.getTableHeader().setReorderingAllowed(false);

		

		jTable1.setModel(new javax.swing.table.DefaultTableModel(inhoud,
				new String[] { "Behandelcode", "Therapienaam","Therapeut","TherapieSoort", "Duur",
				"Prijs" ,"Omschrijving"}));

		jScrollPane1.setViewportView(jTable1);
		jTable1.setPreferredScrollableViewportSize(new Dimension(700, 600));
		jTable1.setFillsViewportHeight(true);
		
		/**
		 * MouseListener, zodat de gegevens uit de column waar op geklikt wordt
		 * geopend wordt in een nieuw frame zodat de gegeven beter te lezen zijn
		 */
		jTable1.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e){
				if (e.getComponent().isEnabled() && e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2){
					Point p = e.getPoint();
					int row = jTable1.rowAtPoint(p); 
					int column = jTable1.columnAtPoint(p); 

					Object obj = jTable1.getModel().getValueAt(row, column);
					if(column != 4 && column != 5){
						String omschr = (String) obj;
						new WeergavePopup(omschr);
					}
				}
			}
		});
		
		jButton1.setText("toevoegen");

		jButton2.setText("Wijzigen");

		jButton3.setText("Verwijderen");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING,
												false)
												.addComponent(
														jButton3,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														jButton2,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														jButton1,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE))
								.addContainerGap(563, Short.MAX_VALUE))
				.addGroup(
						layout.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
								.addGroup(
										layout.createSequentialGroup()
												.addGap(0, 105, Short.MAX_VALUE)
												.addComponent(
														jScrollPane1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addGap(0, 105, Short.MAX_VALUE))));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(jButton1)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jButton2)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jButton3)
								.addContainerGap(335, Short.MAX_VALUE))
				.addGroup(
						layout.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
								.addGroup(
										layout.createSequentialGroup()
												.addGap(0, 0, Short.MAX_VALUE)
												.addComponent(
														jScrollPane1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addGap(0, 0, Short.MAX_VALUE))));
	}

	@Override
	public void update(Observable o, Object arg) {
		this.inhoud = null;
		jTable1.setModel(new javax.swing.table.DefaultTableModel(inhoud,
				new String[] { "Behandelcode", "Therapienaam","Therapeut","TherapieSoort", "Duur",
				"Prijs" ,"Omschrijving"}));
		
		PraktijkLijst lijst = (PraktijkLijst) o;
	
		int aantaltherapienen = 0;
		for (int x = 0; x < lijst.getPraktijken().size(); x++) {
			for (int y = 0; y < lijst.getPraktijken().get(x).getTherapeuten().size(); y++){
					for (int z = 0; z < lijst.getPraktijken().get(x).getTherapeuten().get(y).getTherapieen().size(); z++){
						aantaltherapienen++;
			
					}
			}
		}
		int aantal = 0;
		Object[][] inhoud = new Object[aantaltherapienen][7];
		for (int a = 0; a < lijst.getPraktijken().size(); a++) {

			for (int b = 0; b < lijst.getPraktijken().get(a).getTherapeuten().size(); b++) {
				for(int c = 0; c < lijst.getPraktijken().get(a).getTherapeuten().get(b).getTherapieen().size(); c++){
			

				inhoud[aantal][0] = (Object) lijst.getPraktijken().get(a)
						.getTherapeuten().get(b).getTherapieen().get(c).getBehandelcode();
				inhoud[aantal][1] = (Object) lijst.getPraktijken().get(a)
						.getTherapeuten().get(b).getTherapieen().get(c).getNaam();
				inhoud[aantal][2] = (Object)lijst.getPraktijken().get(a)
						.getTherapeuten().get(b).getVoorNaam()+ "-" + lijst.getPraktijken()
						.get(a).getTherapeuten().get(b).getBsn();
				inhoud[aantal][3] = (Object) lijst.getPraktijken().get(a)
						.getTherapeuten().get(b).getTherapieen().get(c).getSoort();
				inhoud[aantal][4] = (Object) lijst.getPraktijken().get(a)
						.getTherapeuten().get(b).getTherapieen().get(c).getDuur();
				inhoud[aantal][5] = (Object) lijst.getPraktijken().get(a)
						.getTherapeuten().get(b).getTherapieen().get(c).getPrijs();
				inhoud[aantal][6] = (Object) lijst.getPraktijken().get(a)
						.getTherapeuten().get(b).getTherapieen().get(c).getOmschijving();
				
				aantal++;
				}
				jTable1.setModel(new javax.swing.table.DefaultTableModel(inhoud,
						new String[] { "Behandelcode", "Therapienaam","Therapeut","TherapieSoort", "Duur",
						"Prijs" ,"Omschrijving"}));
				}
		}
		}
	

	public String getSelectedbehandelcode() {
		if(jTable1.getSelectedRow() <0){
			return "fout";
		}
		else {
			return jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString();
		}

	}
	public String getSelectedtherapeut() {
		if(jTable1.getSelectedRow() <0){
			return "fout";
		}
		else {
			return jTable1.getValueAt(jTable1.getSelectedRow(), 2).toString();
		}

	}
}
